<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/7
 * Time: 15:57
 */
include "config.php";

if (!empty($_REQUEST["pid"])&&!empty($_REQUEST["num"])&&!empty($_REQUEST["price"])&&!empty($_REQUEST["title"])){
    $pid=$_REQUEST["pid"];
    $num=$_REQUEST["num"];
    $price=$_REQUEST["price"];
    $title=$_REQUEST["title"];
    $img=$_REQUEST["img"];
    $uname=$_REQUEST["uname"];
    $total=$num*$price;
    //查询该用户该产品是否已存在
    $searchSQL="SELECT *FROM bangg_net.`goodinfo` WHERE u_name=? AND p_id=?";
    $stmt=$conn->prepare($searchSQL);
    $stmt->bind_param("ss",$uname,$pid);
    $stmt->execute();
    $result=$stmt->get_result();
    if ($result->num_rows>=1){

        $updataSQL="UPDATE bangg_net.`goodinfo` SET p_num=p_num+?,p_total=p_num*p_price WHERE u_name=? AND p_id=?";
        $stmt->prepare($updataSQL);
        $stmt->bind_param("sss",$num,$uname,$pid);
        $stmt->execute();

        print_r(json_encode(Array("status"=>1,"msg"=>"更新成功")));
    }else{

        $addcarSQL="INSERT	INTO bangg_net.`goodinfo`(u_name,p_id,p_img,p_title,p_price,p_num,p_total)VALUES(?,?,?,?,?,?,?)";
        $stmt=$conn->prepare($addcarSQL);
        $stmt->bind_param("sssssss",$uname,$pid,$img,$title,$price,$num,$total);
        $stmt->execute();
        if ($stmt->affected_rows>=1){
            print_r(json_encode(Array("status"=>1,"msg"=>"加入成功")));
        }else{
            print_r(json_encode(Array("status"=>0,"msg"=>"加入失败")));
        }

    }
}else{
    print_r(Array("status"=>0,"msg"=>"参数错误"));
}